//
//  SlideSwitchView.m
//  
//
//  Created by kiefer on 14-1-3.
//  Copyright (c) 2014年 kiefer. All rights reserved.
//

#import "SlideSwitchView.h"

@implementation SlideSwitchView


- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initialize];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

#pragma mark - 初始化

- (void)initialize
{
    self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
    _scrollView.backgroundColor = [UIColor clearColor];
    _scrollView.userInteractionEnabled = YES;
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.delegate = self;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
    [_scrollView.panGestureRecognizer addTarget:self action:@selector(handlePanGesture:)];
    [self addSubview:_scrollView];
    
    _viewArray = [[NSMutableArray alloc] init];
    _contentOffsetX = 0;
    _selectedIndex = 0;
}

- (void)reloadData
{
    NSUInteger number = [self.delegate numberOfTabs:self];
    for (int i = 0; i < number; i++) {
        UIViewController *controller = [self.delegate slideSwitchView:self viewOfTab:i];
        [_viewArray addObject:controller];
        [_scrollView addSubview:controller.view];
    }
    [self setNeedsLayout];
    
    //选中第一个view
    if ([self.delegate respondsToSelector:@selector(slideSwitchView:didSelectTab:)]) {
        [self.delegate slideSwitchView:self didSelectTab:self.selectedIndex];
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat boundsW = self.bounds.size.width;
    CGFloat boundsH = self.bounds.size.height;
    
    _scrollView.frame = self.bounds;
    _scrollView.contentSize = CGSizeMake(boundsW * [_viewArray count], 0);
    _scrollView.contentOffset = CGPointMake(boundsW * self.selectedIndex, 0);
    
    for (int i = 0; i < [_viewArray count]; i++) {
        UIViewController *controller = _viewArray[i];
        controller.view.frame = CGRectMake(boundsW * i, 0, boundsW, boundsH);
    }
}

- (void)handlePanGesture:(UIPanGestureRecognizer *)panGesture
{
    //当滑道左边界时，传递滑动事件给代理
    if(_scrollView.contentOffset.x <= 0) {
        if ([self.delegate respondsToSelector:@selector(slideSwitchView:panLeftEdge:)]) {
            [self.delegate slideSwitchView:self panLeftEdge:panGesture];
        }
    } else if (_scrollView.contentOffset.x >= (_scrollView.contentSize.width - _scrollView.bounds.size.width)) {
        if ([self.delegate respondsToSelector:@selector(slideSwitchView:panRightEdge:)]) {
            [self.delegate slideSwitchView:self panRightEdge:panGesture];
        }
    }
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex
{
    if (_selectedIndex != selectedIndex) {
        _selectedIndex = selectedIndex;
        
        [_scrollView setContentOffset:CGPointMake(self.bounds.size.width * selectedIndex, 0) animated:YES];
        
        if ([self.delegate respondsToSelector:@selector(slideSwitchView:didSelectTab:)]) {
            [self.delegate slideSwitchView:self didSelectTab:selectedIndex];
        }
    }
}

//滚动视图开始时
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    _contentOffsetX = scrollView.contentOffset.x;
}

//滚动视图结束
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //判断用户是否左滚动还是右滚动
    if (_contentOffsetX < scrollView.contentOffset.x) {
        _isLeftScroll = YES;
    } else {
        _isLeftScroll = NO;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _selectedIndex = (int)scrollView.contentOffset.x/self.bounds.size.width;
    
    if ([self.delegate respondsToSelector:@selector(slideSwitchView:didSelectTab:)]) {
        [self.delegate slideSwitchView:self didSelectTab:self.selectedIndex];
    }
}

@end
